#include<iostream>
#include<vector>
using namespace std;

vector<pair<int,int>> v1(110);

void slove()
{
	int n;
	cin >> n;

	int max = -1;
	int maxi = -1;
	int flag = 0;
	for (int i = 0; i < n; i++)
	{
		int s,e;
		cin >> s>>e;

		v1[i] = {s,e};
	}

	int des = 0;
	for (int i = 1; i < n; i++)
	{
		if (v1[des].first > v1[i].first)
		{
			continue;
		}
		else
		{
			if (v1[des].second > v1[i].second)
			{
				continue;
			}
			else
			{
				flag = 1;
				break;
			}
		}
	}

	if (flag)
	{
		cout << "-1" << endl;
	}
	else
	{
		cout << v1[des].first << endl;
	}

}
int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		slove();
	}
	return 0;
}